<?php
/**
 * 级联选择器组件
 * 提供请求配置，数据加载的服务
 * @author yangdongqi <yangdongqi@hayzone.com>
 * @package lib.components.linkage
 */
class CLinkageSelectorHandler extends CApplicationComponent {
	
	/**
	 * 请求Controller的配置
	 * <pre>
	 * array(
	 *  // 名称
	 *  'name' => 'ls',
	 *  // 使用的类
	 *  'class' => 'CLinkageSelectorController',
	 * );
	 * </pre>
	 * @var array
	 */
	public $controller = array(
		'name' => 'ls',
		'class' => 'CLinkageSelectorController',
	);
	
	/**
	 * 配置文件路径(alias)
	 * 默认路径：'application.config.linkage'
	 * @var string
	 */
	public $configFielAlias = 'application.config.linkage';
	
	/**
	 * 配置内容
	 * @var array
	 */
	protected $_config;
	
	/**
	 * 加载配置文件
	 * @return array
	 */
	public function config() {
		if(!$this->_config) {
			$this->_config = include Yii::getPathOfAlias($this->configFielAlias) . '.php';
		}
		return $this->_config;
	}
	
	/**
	 * 初始化controller
	 * (non-PHPdoc)
	 * @see CApplicationComponent::init()
	 * @return void
	 */
	public function init() {
		parent::init();
		Yii::app()->controllerMap = array_merge(Yii::app()->controllerMap, array(
			$this->controller['name'] => $this->controller['class'],
		));
	}
	
	/**
	 * 获取请求地址
	 * @return string
	 */
	public function getRoute() {
		return '/'.$this->controller['name'].'/fill';
	}
	
	/**
	 * 获取配置
	 * @param string $name 配置名
	 * @return array
	 */
	public function getConfig($name) {
		$config = $this->config();
		return $config[$name];
	}
	
	/**
	 * 加载模型数据
	 * @param string $name 配置名称
	 * @param int $layer
	 * @param int $value
	 * @return array
	 */
	public function loadData($name, $layer, $value) {
		$config = $this->getConfig($name);
		
		$modelName = $config['modelName'];
		$criteria = isset($config['criteria']) ? $config['criteria'] : array();
		$model = new $modelName();
		$defaultData = array('' => '请选择');
		
		$findAttributes = array(
			'layer' => $layer,
		);
		if($value && $layer > 1) {
			$findAttributes['parent_id'] = $value;
		}
		if($layer > 1 && !$value) {
			return array(
				'itemCount' => 0,
				'data' => $defaultData,
			);	
		}
		
		$data = $model->findAllByAttributes($findAttributes,$criteria);
		
		foreach($data as $one) {
			$defaultData[$one->{$config['valueField']}] = $one->{$config['textField']};
		}
		return array(
			'itemCount' => count($data),
			'data' => $defaultData,
		);	
	}
	
	/**
	 * 加载HTML视图
	 * @param string $name 配置名称
	 * @param int $layer
	 * @param int $value
	 * @return string
	 */
	public function loadHtml($name, $layer, $value) {
		$config = $this->getConfig($name);
		$data = $this->loadData($name, $layer, $value);
		$html = '';
		foreach($data['data'] as $k=>$v) {
			$htmlOptions = array(
				'value' => $k,
			);
			$html .= CHtml::tag('option', $htmlOptions, $v);
		}
		
		return array(
			'html' => $html,
			'itemCount' => $data['itemCount']
		);
	}
	
	public function getNodePath($name, $value) {
		$config = $this->getConfig($name);
		$model = new $config['modelName'];
		$model = $model->findByPk($value);
		$data = $model->getAncestors();
		return $this->formatData($config, $data);
	}
	
	public function formatData($config, $data, $defaultData = array()) {
		foreach($data as $one) {
			$defaultData[$one[$config['valueField']]] = $one[$config['textField']];
		} 
		return $defaultData;
	}
}

/**
 * CLinkageSelectorController
 * 支持级联选择框的请求
 * @author yangdongqi <yangdongqi@hayzone.com>
 * @package lib.components.linkage
 */
class CLinkageSelectorController extends CController {
	public function actionFill($name, $layer, $value) {
		echo CJSON::encode(Yii::app()->linkageSelector->loadHtml($name, $layer, $value));
	}
}


?>